﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

/// <summary>
/// 运输计价表操作插件
/// </summary>
/// <remarks>
/// 功能：1、同一路线，只有一个有效的计价表
/// 2、审核后将之前路线价格的有效截止日自动变成当前日期开始前一天
/// 创建人：lxl
/// 创建日期：2023-12-27
/// 修改信息（1）：修改人，修改日期，修改内容
/// </remarks>
namespace LM.YF.K3Cloud.Plugin
{
    [HotUpdate, Description("运输计价表操作插件")]
    public class ShipRutePriceService : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            //将需要应用的字段Key加入

            e.FieldKeys.Add("F_RDLV_ShipRute");//运输路线
            e.FieldKeys.Add("F_RDLV_StartDate");//有效开始日期
            e.FieldKeys.Add("F_RDLV_EndDate");//有效结束日期
            e.FieldKeys.Add("F_RDLV_ShipCode");//运输计划点
        }

        public override void BeforeDoSaveExecute(BeforeDoSaveExecuteEventArgs e)
        {
            base.BeforeDoSaveExecute(e);
        }

        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            if (this.FormOperation.Operation == OperationNumberConst.OperationNumber_Save)
            {
                foreach (ExtendedDataEntity entitys in e.SelectedRows)
                {
                    //获取运输路线
                    string F_RDLV_ShipRute = Convert.ToString(entitys["F_RDLV_ShipRute_id"]);
                    //获取运输计划点
                    string F_RDLV_ShipCode = Convert.ToString(entitys["F_RDLV_ShipCode_id"]);
                    //获取有效开始日期
                    string F_RDLV_StartDate = Convert.ToString(entitys["F_RDLV_StartDate"]);
                    //获取有效结束日期
                    string F_RDLV_EndDate = Convert.ToString(entitys["F_RDLV_EndDate"]);
                    //当前单据fid
                    string fid = Convert.ToString(entitys["id"]);
                    //获取该区间内其他再用的运输计价表
                    string strSql = $@"select a.fid
                                from RDLV_t_ShipRutePrice a where
                                a.FForbidStatus='A' 
                                and
                                (
                                Convert(datetime,'{F_RDLV_StartDate}') <= F_RDLV_StartDate 
                                )
                                and a.F_RDLV_ShipRute={F_RDLV_ShipRute} and F_RDLV_ShipCode={F_RDLV_ShipCode} and fid !={fid}";
                    DynamicObjectCollection frutePrice = DBUtils.ExecuteDynamicObject(this.Context, strSql);
                    if (frutePrice != null && frutePrice.Count > 0)
                    {
                        e.Cancel = true;
                        throw new Exception("该运输路线有效日期和其他路线期间重合,或日期小于当前路线开始日期，请重新维护");
                    }
                }
            }
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            if (this.FormOperation.Operation == OperationNumberConst.OperationNumber_Audit)
            {
                //保存后将该计价表的前一个有效期置为当前日期的前一天
                foreach (DynamicObject entitys in e.DataEntitys)
                {
                    //获取运输路线
                    string F_RDLV_ShipRute = Convert.ToString(entitys["F_RDLV_ShipRute_id"]);
                    //获取运输计划点
                    string F_RDLV_ShipCode = Convert.ToString(entitys["F_RDLV_ShipCode_id"]);
                    //获取有效开始日期
                    DateTime F_RDLV_StartDate = Convert.ToDateTime(entitys["F_RDLV_StartDate"]);
                    //开始时间前一天
                    DateTime F_RDLV_StartDateAgo = F_RDLV_StartDate.AddDays(-1);
                    //fid
                    string fid = Convert.ToString(entitys["id"]);
                    //获取该区间内其他再用的运输计价表
                    string strSql = string.Format(@"update
                    RDLV_t_ShipRutePrice  set F_RDLV_ENDDATE='{0}'
                    where FForbidStatus='A'  and FDOCUMENTSTATUS='C' and 
                    Convert(datetime,F_RDLV_ENDDATE)='9999-12-31 00:00:00.000'
                    and fid<>'{1}'
                    and F_RDLV_ShipRute={2} and F_RDLV_ShipCode={3}  ", F_RDLV_StartDateAgo, fid, F_RDLV_ShipRute, F_RDLV_ShipCode);
                    int rows = DBUtils.Execute(this.Context, strSql);
                }
            }
        }
    }
}
